import time
import json
import board
from adafruit_bme280 import basic as adafruit_bme280

def read_sensor():
    i2c = board.I2C()
    sensor = adafruit_bme280.Adafruit_BME280_I2C(i2c)
    return {
        "temp": round(sensor.temperature, 1),
        "humidity": round(sensor.humidity, 1),
        "pressure": round(sensor.pressure / 100, 1)  
    }

def save_data(data):
    with open("env_data.json", "w") as f:
        json.dump(data, f)
    print("Save!")

if __name__ == "__main__":
    try:
        while True:
            env_data = read_sensor()
            save_data(env_data)

            print(f"wendu: {env_data['temp']}")
            print(f"shidu: {env_data['humidity']}%")
            print(f"qiya: {env_data['pressure']}hPa")
            print("="*20)
            
            time.sleep(300)  
            
    except KeyboardInterrupt:
        print("All Stop!")